/**
 * @version 2007-3-28
 * @author xalinx at gmail dot com
 */
package org.nanhill.jvm.demo;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
 * @version 2007-3-28
 * @author xalinx at gmail dot com
 * 
 */
public class SerializePerformance {
	private static class TestBean implements Serializable {

		/**
		 * 
		 */
		private static final long serialVersionUID = -6461192167843152131L;

		private String name;

		private String pass;

		private int age;

		public int getAge() {
			return age;

		}

		public void setAge(int age) {
			this.age = age;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getPass() {
			return pass;
		}

		public void setPass(String pass) {
			this.pass = pass;
		}

	}

	public static void testMethod(Object obj) throws Exception {
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		ObjectOutputStream outStream = new ObjectOutputStream(byteStream);
		outStream.writeObject(obj);
		outStream.flush();
		outStream.close();
		byteStream.close();
	}

	public static void main(String[] args) throws Exception {
		TestBean bean = new TestBean();
		bean.setName("fankai");
		bean.setPass("fankai");
		bean.setAge(30);
		testMethod(bean);

		long start = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			testSerialize(i);
		}
		long end = System.currentTimeMillis();
		System.out.println(end - start);
	}

	public static void testSerialize(int i) throws Exception {
		TestBean bean = new TestBean();
		bean.setName("fankai" + i);
		bean.setPass("fankai" + i);
		bean.setAge(i);
		ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
		ObjectOutputStream outStream = new ObjectOutputStream(byteStream);
		outStream.writeObject(bean);
		outStream.flush();
		outStream.close();
		byteStream.close();
	}

}
